<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Table } from '@/components/Table'
import { useTable } from '@/hooks/web/useTableTest'
import { reactive, PropType } from 'vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useCache } from '@/hooks/web/useCache'
import { spDrugStockInOutLogListApi } from '@/api/sp/drug/index'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  },
  titleData: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  }
})

const spDrugStockInOutLogList = (val) => {
  if (props.currentRow.batch_id) {
    val.batch_id = props.currentRow.batch_id
  } else {
    val.drug_id = props.titleData.id
  }
  return spDrugStockInOutLogListApi(val)
}
const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: spDrugStockInOutLogList,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { getList, setSearchParams } = methods

getList()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: '主键',
    fixed: 'left',
    width: 60,
    table: {
      show: true
    },
    form: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'drug_name',
    label: '药品名称',
    width: 200,
    search: {
      show: true
    }
  },
  {
    field: 'batch_code',
    label: '药品批次',
    width: 200,
    search: {
      show: true
    }
  },
  {
    field: 'code',
    label: '药品出入库编号',
    width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'in_out_type',
    label: '类型',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('in_out_type', cellValue)
    },
    table: {
      show: true
    }
  },
  {
    field: 'out_type',
    label: '出库类型',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('out_type', cellValue)
    },
    table: {
      show: true
    }
  },
  {
    field: 'in_type',
    label: '入库类型',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('in_type', cellValue)
    },
    table: {
      show: true
    }
  },
  {
    field: 'number',
    label: '数量',
    width: 150,
    search: {
      show: false
    }
  },
  {
    field: 'create_time',
    label: '创建时间',
    width: 200,
    form: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    search: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        'is-range': true,
        'unlink-panels': true,
        rangeSeparator: 'To',
        type: 'daterange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
      :selection="false"
    />
  </ContentWrap>
</template>
